<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$user = $this->session->userdata('rsh_user');
		if(!$user['user_id']){
			$this->js->go("/login/index.html");
			exit;
		}
		$this->tab = 'user';
		$this->sub = 'user';
	}
	
	public function index(){
		self::zhs_center();
	}
	
	
	public function logout(){
		setcookie("zhsjr_online",false,0,'/');
		//$this->session->unset_userdata('rsh_user');
		$this->session->sess_destroy();
		$this->js->go('/user.html');
	}
	

	
	/**
	 * 用户中心
	 */
	public function zhs_center(){

		// 标的安全等级
		$this->load->model('Conset_model');
		$this->load->model('Borrow_model');
		$data['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();	
		$data['borrow_style'] 	= $this->Borrow_model->arrays('borrow_style');//还款方式
		$data['borrow_bill_bank'] 				= $this->Borrow_model->arrays('borrow_bill_bank');//银行
		$data['borrow_status'] 				= $this->Borrow_model->arrays('borrow_status');//状态
		$data['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标类型
		$user = $this->session->userdata('rsh_user');
		$adata['page_start'] = intval($this->input->get('per_page'));//intval($page);
		$adata['page_size'] = 8;
		$adata['where']['user_id'] 	= $user['user_id'];
		$adata['where']['fstatus'] 	= "0,1";
			
		// 成功投资的标
		// $index_borrow_list_key = 'rsh_center_tender_list';
		
		$tender_vdata= $this->Borrow_model->borrow_tender_user_list($adata);
		//投标实际总金额
		$sj_account = 0;
		if( !empty($tender_vdata['tender_list']) ){
			foreach( $tender_vdata['tender_list'] as $k => $v ){
				$sj_account += $v['account'];
			}
		}
		$data['tender_money_count'] = $sj_account;
		$data['tender_list']= $tender_vdata['tender_list'];//成功投资的列表
		
		//$this->mycache->set_array($index_borrow_list_key,$bdata3,60);
		
		$this->load->library('pagination');
		$config['base_url'] = '/user/zhs_center.html?order=';
		$config['total_rows'] = $tender_vdata['tender_count'];
		$config['per_page'] = 8;
		//$config['page_query_string'] = false;
		
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		
		$this->pagination->initialize($config);		

		// 正在投资的借款标
		$index_borrow_list_key = 'rsh_center_borrow_list';
		$bdata2 = null;
		if (!$bdata2){
			$adata['page_size']  = 5;
			$adata['page_start'] = 0;
			$now_vdata = $this->Borrow_model->get_borrow_blist($adata);
			//投标实际总金额
			$sj_account = 0;
			if( !empty($now_vdata['borrow_list']) ){
				foreach( $now_vdata['borrow_list'] as $k => $v ){
					if($v['account'] <= $v['account_yes']) $v['fstatus'] = 3;
				}
			}
			$data['tender_money_count'] = $sj_account;
			$bdata2['borrow_list']= $now_vdata['borrow_list'];//成功投资的列表
		}
		$data['borrow_list'] = $bdata2['borrow_list'];
		
		$data['user_sub'] = 'tender_myinv';
		$data['sub'] = 'tender';
		//$data['addup']= $safeinfo['addup'];
		$data['title'] 						= "用户中心".' - '.CON_KEYWORDS; 
		$data['tab'] 						= "user"; 
		$data['sub_1'] = "会员中心";
		$data['sub_1_href'] = "/user/zhs_center.html";
		$data['sub_2'] = "我的投资";
		$data['sub_2_href'] = "/user/zhs_center.html";
		$this->load->view('user/zhs_user_center',$data);
	}
	
	/**
	 * 实名认证
	 */
	public function zhs_realname(){

		$data = array();
		$act = $this->input->post('act');
		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model', 'user');
		$this->load->model('Area_model', 'area');
		$this->load->model('Linkage_model', 'linkage');
		$this->load->model('User_info_model', 'user_info');
		$data = $this->get_user_info('user_realname','safe');

		if(isSet($data['user']['real_status']) && $data['user']['real_status'] == '2'){
			$this->js->go('/user/zhs_user_safe.html');exit;
		}
		if($act == 'submit'){
			$ret = array(
				'msg' => '',
				'url' => '',
			);
			
			
			$setArr = array(
				'realname' => $this->input->post('realname'),
				'sex'  => $this->input->post('sex'),
				//'nation' => $this->input->post('nation'),
				'birthday' => "",
				'card_type' => 1,//默认使用身份证
				'card_id' => $this->input->post('card_id'),
				//'province' => $this->input->post('province'),
				//'city' => $this->input->post('city'),
				//'area' => $this->input->post('area'),
				//'area' => $this->input->post('area'),
				//'qq' => $this->input->post('qq'),
			);
			
			/* $thumbnail = Array();
			$file_upload  =  "f_file";		
			$thumbnail = $this->upload_borrow_thumbnail(UPFILES_BORROW_DEAL,$file_upload); */
			
		  //echo "34131";exit;	
			if(!$setArr['realname']){
				$ret['msg'] = '请填写真实姓名';
			}elseif(!$setArr['sex']){
				$ret['msg'] = '请选择性别';
			}elseif(!$setArr['card_id']){
				$ret['msg'] = '请填写证件号码';
			}elseif(!$this->myformcheck->isChinese($setArr['realname'])){
				$ret['msg'] = '请填写有效的真实姓名';
			}else{
				$isE = $this->user->get_one(array('card_id'=>$setArr['card_id'],'user_id !='=>$user['user_id']),'user_id');
				if($isE){
					$ret['msg'] = '证件号码已存在';
				}
			}
			if($ret['msg']){
				$this->js->alert($ret['msg']);
				exit;
			}
			// 20150430 身份证正反面上传验证
			$user = $this->user->get_username_by_id($user['user_id']);
			if(!isset($user['realname']) &&  !isset($user['card_id'])) {
				$this->user->update($setArr, array('user_id' => $user['user_id']));
			}
			
			$rest = $this->idcardcheck($setArr['card_id'],$setArr['realname']);
			
			if($rest['ResponseCode'] == '100'){
				$wert = $rest['Identifier'];
				if($wert['Result'] =='一致'){
					$setArr['birthday'] = $wert['Birthday'];
					$setArr['real_status'] = 1;//实名成功
					$this->user->update($setArr, array('user_id' => $user['user_id']));
					$ret['msg'] = '操作成功';
					$this->js->alert($ret['msg']);
					$this->js->go("/user/zhs_center.html");
					exit;
				}
				else $this->js->alert('返回错误结果：'.$wert['Result']);
			}
			else $this->js->alert('返回错误码：'.$rest['ResponseCode']);
			
			//end tangcai
		}else{
			
			//$data['user'] = $this->user->get_by_uid($user['user_id']);
			$data['user_info'] = $this->user_info->get_by_uid($user['user_id']);
			$data['area'] = $this->area->get_list();
			$data['minzu'] = $this->linkage->get_linkage_list(31);
			$data['title'] = "实名认证-安全绑定".' - '.CON_KEYWORDS;
			$data['v'] = $this->user->arrays();
			$data['sub'] = 'user';
			$data['user_sub'] = 'user_safe';
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "安全认证";
			$data['sub_2_href'] = "/user/zhs_user_safe.html";
			$data['sub_3'] = "实名认证-安全绑定";
			$data['sub_3_href'] = "/user/zhs_realname.html";
			$this->load->view('/user/zhs_user_realname',$data);
		}
	}
	
	private function idcardcheck($idcardNum,$realname){
		header("content-type:text/html; charset=utf8");
		$client = new SoapClient ("http://service.sfxxrz.com/IdentifierService.svc?wsdl");
		$r = new stdClass();
		$r->IDNumber = $idcardNum;
		$r->Name = $realname;
		$c = new stdClass();
		$c->UserName = IDCARDUSERNAME;
		$c->Password = IDCARDUSERPWD;
		$result_json  =  $client->SimpleCheckByJson(array("request"=>json_encode($r),"cred"=>json_encode($c)))->SimpleCheckByJsonResult;
		$result = json_decode($result_json, true);
		return $result;
	}
	
	// 上传文件并打水印
	private function upload_borrow_thumbnail($path,$file_upload){
		if(!file_exists($path)){
			mkdir($path,0777);
		}
			
		$config['upload_path'] = $path;
		$config['allowed_types'] = 'jpg|png';
		$config['file_name'] = 'cardpic_'.uniqid();
		$config['overwrite'] = true;
		$config['max_size'] = '512';
		$config['max_width']  = '2048';
		$config['max_height']  = '3000';
		$this->load->library('upload', $config);
		
		$data = $this->upload->multiple($file_upload);
		//echo $file_upload;exit;
		$name = Array();
		$i = 0;
		
		foreach ($data['files'] as $value) {
			$thumbnail = $path.$value['file_name'];
			// 添加水印				
			$this->load->library('image_lib');		
            $warter['source_image'] = $thumbnail;
			$warter['wm_type'] = 'overlay';
			//******  by liub  20141211 修改水印图片，以及水印位置为 底部居右
			$warter['wm_overlay_path'] = WM_OVERLAY_FILE_PNG;
			$warter['wm_opacity'] = '60';
			$warter['wm_vrt_alignment'] = 'B';// 底部
			$warter['wm_hor_alignment'] = 'R';// 右边
			// ****************** end 
			//$this->image_lib->initialize($warter);
			//溥$this->image_lib->watermark();
			$name[$i] = $thumbnail;
			$i++;
		}
		
		return $name;
	}
	
	/**
	 * 邮箱认证
	 */
	public function zhs_email(){
		$data = array();
		$act = $this->input->post('act');
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		$this->load->model('User_model', 'user');
		$data = $this->get_user_info('user_email','safe');
		//$data['user'] = $this->user->get_by_uid($user_id);
		if($act == 'submit'){
			$typer = $this->input->post('typer');
			$ret = array(
				'msg' => '',
				'url' => '',
			);
			$email = $this->input->post('emai');
			if($data['user']['email_status']==1  && !isset($typer)){
				$ret['msg'] = '邮箱验证已通过';
				echo json_encode($ret);
				exit;
			}
			if(isset($typer)){
				if($typer == 'oldemail'){
					$active_id = $this->session->userdata('zhsjr_active_id__email');
					$old_verify_code = $this->input->post('old_verify_code');
					if($active_id != $old_verify_code){
						$ret['msg'] = '旧邮箱验证码不正确';
						echo json_encode($ret);
						exit;
					}
				}
				else  if($typer == 'phone'){
					$active_id = $this->session->userdata($user ['phone']);
					$verify_code = $this->input->post('verify_code');
					if($active_id != $verify_code){
						$ret['msg'] = '手机验证码不正确';
						echo json_encode($ret);
						exit;
					}
				}
			}
			
			if(!$this->myformcheck->isEmail($email)){
				$ret['msg'] = '请输入有效邮箱';
			}else{
				$where = array(
					'user_id !=' => $user_id,
					'email' => $email
				);
				if($this->user->get_one($where, 'user_id')){
					$ret['msg'] = '邮箱已存在';
				}
			}
			
			if($ret['msg']){
				echo json_encode($ret);
				exit;
			}
			
			$setArr = array(
				'email' => $email,
				'email_status' => 2,
			);
			$this->user->update($setArr,array('user_id'=>$user_id));	
			$res = $this->notice_email(10,$user_id);
			
			if($res){
				$ret['cont'] = "邮箱发送成功，请尽快登录邮箱激活";
				echo json_encode($ret);
				exit;
			}else{
				$ret['msg'] = '邮件发送失败，请联系客服';
				echo json_encode($ret);
				exit;
			}
				
			
		}elseif($act == 'apply'){
			$res = $this->notice_email(11,$user_id);
			if($res){
				$ret['info'] =1;
				echo json_encode($ret);
				exit;
			}else{
				$ret['msg'] = '邮件发送失败，请联系客服';
				echo json_encode($ret);
				exit;
			}
		}else{
			$data['title'] = "邮箱认证".' - '.CON_KEYWORDS;
			$data['sub'] = 'user';
			$data['user_sub'] = 'user_safe';
			
			
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "安全认证";
			$data['sub_2_href'] = "/user/zhs_user_safe.html";
			$data['sub_3'] = "邮箱认证";
			$data['sub_3_href'] = "/user/zhs_email.html";
			$this->load->view('user/zhs_user_email',$data);
		}
	}
	
	/**
	 * 手机认证
	 */
	public function zhs_phone(){

		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model', 'user');
		$user_id = $user['user_id'];
		$this->mycache->delete('rsh_user_'.$user_id);
		$data = $this->get_user_info('user_phone','safe');
		if($data['user']['real_status']  != '0'){
			$this->js->go('/user/zhs_user_safe.html');
			exit;
		}
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		
		if($this->input->post('act') == 'apply'){//申請
			$k = md5('zhsjr');
			if(isset($_COOKIE[$k]) && $_COOKIE[$k] == 2){
				$ret['msg'] = '3分钟内只能获取一次短信验证码，请稍后再试';
				echo json_encode($ret); 
				exit;				
			}else{
				$phone = $this->input->post('phone');
				if(!$this->myformcheck->isPhone($phone)){
					$ret['msg'] = '请填写有效的手机号码';
				}else{
					// 验证手机号码是否存在
					$tmpwhere = array(
						'phone'=>$phone
					);
					$row = $this->user->get_one($tmpwhere,'user_id');
					if(isset($row['user_id'])){
						$ret['msg'] = '手机号码已存在';
					}
				}
				if($ret['msg']){
					echo json_encode($ret); 
					exit;				
				}
				$ctime = time()+3*60;//300秒内只能发送一次短信息
				setcookie($k,1,$ctime);
				
				$md5 = md5($user['user_id'].$phone);
				$md5 = substr($md5,3,6);
				$msg = "您的手机验证码为：".$md5;
				$this->load->library('phone');
				$res = $this->phone->send($phone,$msg);
				if($res == '0'){
					
					$this->user->update ( array ('smscode' => $md5, 'smstime' => time () ), array ('user_id' => $user['user_id'] ) );
					
					$ret['msg'] = '手机验证码已发送，请注意查收';
					echo json_encode($ret); 
					exit;
				}else{
					$ret['msg'] = '短信发送失败，请稍后再试或联系客服';
					echo json_encode($ret); 
					exit;
				}
			}
		}elseif($this->input->post('act') == 'verify'){//更换验证
			$verify_code = $this->input->post('verify_code');
			$phone = $this->input->post('phone');
			$md5 = substr(md5($user_id.$phone),3,6);
			if($verify_code == $md5){
				$typer = $this->input->post('typer');
				if($typer == 'oldphone'){//根据旧手机验证码更换
					$md5 = $this->session->userdata ($user ['phone']);
					$old_verify_code = $this->input->post('old_verify_code');
					if($old_verify_code != $md5){
						$ret['msg'] = '旧手机验证码错误';
						echo json_encode($ret);
						exit; 
					}
				}
				elseif($typer == 'paypw'){//根据交易手机验证码更换
					$cardid = $this->input->post('cardid');
					$paypassword = $this->input->post('paypassword');
					// 验证手机号码是否存在
					$tmpwhere = array(
						'card_id'=>$cardid,
						'paypassword'=>md5(md5($paypassword))
					);
					$row = $this->user->get_one($tmpwhere,'user_id');
					if(!isset($row['user_id'])){
						$ret['msg'] = '身份证或者交易密码错误';
						echo json_encode($ret);
						exit; 
					}
				}
				$setArr = array(
					'phone_status' => 1,
					'phone'=>$phone
				);
				$res = $this->user->update($setArr,array('user_id'=>$user_id));
				if($res){
					
					$user['phone'] = $phone;
					$this->send_msg(array('user_id'=>$user_id,sys_id=>18));//发送代金卷站内信
					//$this->notice_credit($user_id, 'phone');
					$ret['msg'] = '验证通过';
					$ret['url'] = '/user/zhs_phone';
					echo json_encode($ret);
					exit; 
				}else{
					$ret['msg'] = '网络忙，请稍后再试！';
					echo json_encode($ret);
					exit;
				}
			}else{
				$ret['msg'] = '新手机验证码错误';
				echo json_encode($ret);
				exit;
			}
			
		}else{//展示页面
			$data['sub'] = 'user';
			$data['user_sub'] = 'user_safe';
			$data['title'] = "手机认证".' - '.CON_KEYWORDS;
			
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "安全认证";
			$data['sub_2_href'] = "/user/zhs_user_safe.html";
			$data['sub_3'] = "手机认证";
			$data['sub_3_href'] = "/user/zhs_phone.html";
			
			$this->load->view('user/zhs_user_phone',$data);
		}
	}


	/**
	 * 上传头像
	 */
	public function zhs_photo($act = ''){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model', 'user');
		$user_id = $user['user_id'];
		$data['user'] = $this->user->get_by_uid($user_id);
		
		if($act == 'upfile'){
			$file_src = "src.png"; 
			$filename162 = "./static/data/avatar/".$user_id."_avatar_big.jpg"; 
			$filename48 = "./static/data/avatar/".$user_id."_avatar_middle.jpg";
			$filename20 = "./static/data/avatar/".$user_id."_avatar_small.jpg"; 
			
			$src=base64_decode($_POST['pic']);
			$pic1=base64_decode($_POST['pic1']);   
			$pic2=base64_decode($_POST['pic2']);  
			$pic3=base64_decode($_POST['pic3']);  
			
			if($src) {
				file_put_contents($file_src,$src);
			}
			
			file_put_contents($filename162,$pic1);
			file_put_contents($filename48,$pic2);
			file_put_contents($filename20,$pic3);
			
			$rs['status'] = 1;
			
			print json_encode($rs);
		}else{
			$data = $this->get_user_info('user_photo','safe');
			$data['title'] = "上传头像".' - '.CON_KEYWORDS;
			$data['sub_1'] = "上传头像";
			$data['sub_1_href'] = "/user/zhs_photo.html";
			$this->load->view('user/zhs_user_photo',$data);
		}
	}

	/**
	 * 用户详细信息
	 * @param string $act 
	 */
	public function zhs_info($act=''){
		$data = array();
		
		$submit = $this->input->post('submit');
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		
		$this->load->model('User_model', 'user');
		$this->load->model('User_info_model', 'userinfo');
		$data = $this->get_user_info('','user_base');
		$data['act']=$act;
		//$data['user'] = $this->user->get_by_uid($user_id);
		$data['userinfo'] = $this->userinfo->get_by_uid($user_id);
		if($act == 'contact'){
			//education_record
			if($submit == 1){
				$username0 = trim($this->input->post('username0'));
				if($user['username'] != $username0){
					if($username0=='' || $this->myformcheck->isNumber($username0)){
						$ret['msg'] = '会员昵称不允许纯数字或者为空';
						$ret['url'] = '';
						echo json_encode($ret);
						exit;
					}
					$is_username= $this->user->check_username($username0);
					if($is_username){
						$ret['msg'] = '此会员昵称已经存在';
						$ret['url'] = '';
						echo json_encode($ret);
						exit;
					}
					else {
						$user['username']= $username0;
						$this->session->set_userdata('rsh_user',$user);
					}
				}
				
				$setArr = array(
					'education_record' => intval($this->input->post('education_record')),
					'education_school' => $this->input->post('education_school'),
					'education_time1' => strtotime($this->input->post('education_time1')),
					'education_time2' => strtotime($this->input->post('education_time2')),
					'marry' => intval($this->input->post('marry')),
					'child' => intval($this->input->post('child')),
					'safe' => intval($this->input->post('safe')),
					'house' => intval($this->input->post('house')),
					'car' => intval($this->input->post('car')),
					'province' => intval($this->input->post('province')),
					'city' => intval($this->input->post('city')),
					'area' => intval($this->input->post('area')),
					'address' => $this->input->post('address'),
					'tel' => $this->input->post('tel'),
					'qq' => $this->input->post('qq'),
					'wangwang' => $this->input->post('wangwang'),
					'linkman1' => $this->input->post('linkman1'),
					'relation1' => intval($this->input->post('relation1')),
					'phone1' => $this->input->post('phone1'),
					'linkman2' => $this->input->post('linkman2'),
					'relation2' => intval($this->input->post('relation2')),
					'phone2' => $this->input->post('phone2'),
				);
				if($data['userinfo']){
					$res = $this->userinfo->update($setArr,array('user_id'=>$user_id));
				}else{
					$setArr['user_id'] = $user_id;
					$res = $this->userinfo->add($setArr);
				}
				if($setArr['address'] !="" && $setArr['qq'] !=""  && $setArr['linkman1'] !=""  && $setArr['linkman2'] !=""  && $setArr['education_record'] !=""  && $setArr['education_school']!="" ){
					//发放代金券
					$this->load->model('Redmanagement_model', 'redmanagement');
					$this->redmanagement->send_red($user_id,7,"用户信息完整");
				}
				$this->user->update(array('qq'=>$setArr['qq'],'username'=>$username0),array('user_id'=>$user_id));
				$ret['msg'] = '个人详细信息提交成功';
				$ret['url'] = '/user/zhs_info/contact.html';
				echo json_encode($ret);
				exit;
			}
			$data['user_tab'] = 'userinfo_contact';
			$data['title'] = "个人详细信息".' - '.CON_KEYWORDS;
			
		}else{
			$this->js->alert('页面不存在');
			$this->js->go('/user/zhs_info/contact.html');
		}
		$this->load->model('Area_model', 'area');
		$data['area'] = $this->area->get_list();
		$data['sub'] = 'user';
		$data['user_sub'] = "user_msg";
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "个人信息";
		$data['sub_2_href'] = "/user/zhs_info/contact.html";
		$this->load->view('user/zhs_user_info',$data);
	}

	
	
	public function zhs_pwd(){
		$act = $this->input->post('act');
		if($act == 'submit'){
			$ret = array('msg'=>'','url'=>'');
			$oldpwd = $this->input->post('oldpwd');
			$newpwd = $this->input->post('newpwd');
			$newpwd2 = $this->input->post('newpwd2');
			
			if(!isset($newpwd[5])){
				$ret['msg'] = '新密码太短';
				echo json_encode($ret);
				exit;
			}elseif (!$this->myformcheck->isName ($newpwd)) {
				$ret ['msg'] = '无效密码，必须[数字+字母]，其中只允许[_~!#$^&?]字符';
				echo json_encode ($ret);
				exit ;
			}
			if($newpwd == $newpwd2){
				$user = $this->session->userdata('rsh_user');
				$user_id = $user['user_id'];
				$this->load->model('User_model', 'user');
				$user = $this->user->get_by_uid($user_id);
				if(md5(md5($oldpwd)) == $user['password']){
					$md5 = md5(md5($newpwd));
					if($newpwd != $oldpwd && $md5!=$user['paypassword']){
						$res = $this->user->update(array('password'=>$md5),array('user_id'=>$user_id));
						if($res){
							//$this->send_msg(array('user_id'=>$user_id,sys_id=>19));//发送代金卷站内信
							$ret['url'] = '/user/logout/index.html';
							echo json_encode($ret);
							exit;
						}
					}else{
						$ret['msg'] = '新密码不允许同旧密码、登录密码相同';
						echo json_encode($ret);
						exit;
					}
					
				}else{
					$ret['msg'] = '旧密码验证失败,请确定再试';
					echo json_encode($ret);
					exit;
				}
			}else{
				$ret['msg'] = '两次更新密码输入不一致';
				echo json_encode($ret);
				exit;
			}
		}
	
		$data = $this->get_user_info('user_pwd','pwd');
		$data['sub'] = 'user';
		$data['user_sub'] = 'user_safe';
		$data['title'] = "密码设置".' - '.CON_KEYWORDS;
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "安全认证";
		$data['sub_2_href'] = "/user/zhs_user_safe.html";
		$data['sub_3'] = "登录密码";
		$data['sub_3_href'] = "/user/zhs_pwd.html";
		$this->load->view('user/zhs_user_pwd', $data);
	}
	
	public function zhs_paypwd(){
		if(!isset($_SESSION)){
		    session_start();
		}
		$act = $this->input->post('act');
		
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		$this->load->model('User_model', 'user');
		$user = $this->user->get_by_uid($user_id);
		
		$data['user'] = $user;
		if($act == 'submit'){
			$ret = array('msg'=>'','url'=>'');
			$verify_code = $this->input->post('ver_code');
			$md5 = $this->session->userdata ( $user ['phone'] );
			if ($verify_code =="" ){
				$ret['msg'] = '验证码不能为空';
				echo json_encode($ret);
				exit;
			}elseif ($verify_code!=$md5){
				$ret['msg'] = '验证码不正确';
				echo json_encode($ret);
				exit;
			}else{
				$oldpwd = $this->input->post('oldpwd');
				$newpwd = $this->input->post('newpwd');
				$newpwd2 = $this->input->post('newpwd2');
				
				if(!isset($newpwd[5])){
					$ret['msg'] = '新密码太短';
					echo json_encode($ret);
					exit;
				}elseif (!$this->myformcheck->isName ($newpwd)) {
					$ret ['msg'] = '无效密码，必须[数字+字母]，其中只允许[_~!#$^&?]字符';
					echo json_encode ($ret);
					exit ;
				}
				if($newpwd == $newpwd2){
					$tmpPwd = $user['paypassword'] ? $user['paypassword'] : $user['password'];
					if(md5(md5($oldpwd)) == $tmpPwd){
						$md5 = md5(md5($newpwd));
						if($md5 != $tmpPwd && $md5!=$user['password']){
							$res = $this->user->update(array('paypassword'=>$md5),array('user_id'=>$user_id));
							if($res){
								$this->send_msg(array('user_id'=>$user_id,sys_id=>20));//发送代金卷站内信
								$ret['url']= "/user/zhs_user_safe.html";
								echo json_encode($ret);
								exit;
							}
						}else {
							$ret['msg'] = '新密码不允许同旧密码、登录密码相同';
							echo json_encode($ret);
							exit;
						}
						
					}else{
						$ret['msg'] = '原始密码错误';
						echo json_encode($ret);
						exit;
					}
				}else{
					$ret['msg'] = '两次密码输入不一致';
					echo json_encode($ret);
					exit;
				}
			}
		}
		$data['sub'] = 'user';
		$data['user_sub'] = 'user_safe';
		$data['title'] = "交易密码".' - '.CON_KEYWORDS;
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "安全认证";
		$data['sub_2_href'] = "/user/zhs_user_safe.html";
		$data['sub_3'] = "交易密码";
		$data['sub_3_href'] = "/user/zhs_paypwd.html";
		$this->load->view('user/zhs_user_paypwd',$data);
	}
	
	public function zhs_find(){
		if(!isset($_SESSION)){
		    session_start();
		}
		$this->load->helper('captcha');
		$act = $this->input->post('act');
		
		$user = $this->session->userdata('rsh_user');
		$data['user'] = $user;
		
		if($act == 'mail'){
			$ret = array('msg'=>'','url'=>'');
			$captcha = $this->input->post('captcha');
			
			if ($captcha =="" ){
				$ret['msg'] = '验证码不能为空';
				echo json_encode($ret);
				exit;
			}elseif ($captcha!=$_SESSION['captcha']){
				$ret['msg'] = '验证码不正确';
				echo json_encode($ret);
				exit;
			}
			
			$res = $this->notice_email(3,$user['user_id']);
			
			if($res){
				$ret['msg'] = '邮件发送成功，请从邮件内容里的链接地址重新设置交易密码，该链接有效时间1小时，请及时操作！';
			}else{
				$ret['msg'] = '邮箱发送失败，请稍后再试或者联系客服！';
			}
			echo json_encode($ret); 
			exit;
		}
		$data = $this->get_user_info('user_paypwd','pwd');
		
		$data['title'] = "密码找回".' - '.CON_KEYWORDS;
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "安全认证";
		$data['sub_2_href'] = "/user/zhs_user_safe.html";
		$data['sub_3'] = "支付密码找回";
		$data['sub_3_href'] = "/user/zhs_find/index.html";
		$this->load->view('user/zhs_user_find',$data);
	}
	
	//*******************************
	function zhs_invite(){
		$data = array();
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		if($user['user_id']){
                        $page = intval($this->input->get('per_page'));
			$this->load->model('User_model');
			$data = $this->get_user_info('','invite','invite');
			$data['invite_user_list'] = $this->User_model->get_list(array('invite_userid'=>$user_id),'user_id,username,phone,invite_money,add_time');
			$data['url'] = 'http://www.okydt.com/?invite='.$user_id;
                        $invite=$this->User_model->get_invist_num($user_id,$page);
                        //$data['invite']=$invite['count']
                        $this->load->library('pagination');
                        $config['base_url'] ='/user/zhs_invite.html?';
                        $config['total_rows'] = intval($invite['count']);
                        $config['per_page'] = USER_CENTER_PAGE_SIZE;
                        $this->pagination->initialize($config);

			
			$data['title'] = "推广码".' - '.CON_KEYWORDS;
			$data['sub'] = "cps";
			$data['user_sub'] = "cps_user";
			$data['sub_1'] = "理财推广";
			$data['sub_1_href'] = "/user/zhs_invite.html";
			$data['sub_2'] = "推广账号";
			$data['sub_2_href'] = "/user/zhs_invite.html";
            $data['invite_user_list']=$invite['invite_user_list'];
			$this->load->view('user/zhs_user_invite',$data);
		}
	}
	
	/* 推荐用户分层 计算 */
	function invite_layer($layer_id = 0){
		
		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model','user');
		$user =  $this->user->get_by_uid($user['user_id']);
		if($user['is_self'] == '1'){
			$invite_layer = $this->user->arrays('invite_layer');//拿到分层信息
			$this->load->model('borrow_model','borr');
			$keys = array_keys($invite_layer);
			$last_time = date('Ym',strtotime("-1 month"));
			$now_time = date('Ym',time());
			$typer = $this->input->get('typer');
			if(!in_array($layer_id,$keys)){
				$sum = count($invite_layer);
				$users = array($user['user_id']);
				$daa = array();
				$list = array();
				$r = intval(date('m',time()));
				for($i=0; $i<$sum; $i++){
					$list[$keys[$i]] = $users;
					$val = $invite_layer[$keys[$i]];
					$where = array('invite_user'=>$users,'user_invite'=>'1','last_time'=>$last_time,'now_time'=>$now_time );
					$res =  $this->borr->invite_user_account($where);
					$res['invite_user_account_list']['money_ti']=  number_format($res['invite_user_account_list']['money']*$val,2);
					$res['invite_user_account_list']['last_money_ti']=  number_format($res['invite_user_account_list']['last_money']*$val,2);
					$res['invite_user_account_list']['now_money_ti']=  number_format($res['invite_user_account_list']['now_money']*$val,2);
					for($j=1;$j<=$r;$j++){
						$res['invite_user_account_list']['month'.$j.'_money'] = number_format($res['invite_user_account_list']['month'.$j.'_money']/1000,2);
					}
					$res['invite_user_account_list']['layer_id'] = $keys[$i];
					$daa[$keys[$i]] = $res['invite_user_account_list'];
					$users = $res['users_id'];
					if(count($users) == 0) break;
				}
				$this->mycache->set_array('invite_layer_userid'.$user['user_id'],$list);
				if($typer == 'exel'){
					
					$this->load->library('PHPExcel');
					$this->load->library('PHPExcel/IOFactory'); 
					$objPHPExcel = new PHPExcel();
					
					$objPHPExcel->getProperties()->setTitle("推广用户汇总")->setDescription(COMPANY_NAME); 
					$objPHPExcel->setActiveSheetIndex(0);
					$titles = array("推荐等级","推荐用户数","历史投资额","历史提成额","上月投资额","上月提成额","本月投资额","本月提成额");
					for($j = 0; $j < count($titles); $j ++){
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j, 1, $titles[$j]);
					}
					for($j = 0; $j < count($daa); $j++){
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $j+2, "第".($j+1)."级");
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $j+2, $daa[$j+1]['count_userid']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $j+2, $daa[$j+1]['money']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $j+2, $daa[$j+1]['money_ti']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $j+2, $daa[$j+1]['last_money']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $j+2, $daa[$j+1]['last_money_ti']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $j+2, $daa[$j+1]['now_money']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $j+2, $daa[$j+1]['now_money_ti']);
					}
					
					$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
					
					//发送标题强制用户下载文件 
					header('Content-Type: application/vnd.ms-excel'); 
					header('Content-Disposition: attachment;filename="推广用户汇总.xls"'); 
					header('Cache-Control: max-age=0'); 
					$objWriter->save('php://output'); 
					return;
				}
				$data['invi_user'] = $daa;
			}
			else {
				$page = intval($this->input->get('per_page'));
				if(!$page) $page = 0;
				$list = $this->mycache->get_array('invite_layer_userid'.$user['user_id']);
				if(isset($list[$layer_id])){
					if($typer == 'exel'){
						$page = null;
					}
					$where = array('invite_user'=>$list[$layer_id],'user_details'=>'1','last_time'=>$last_time,'now_time'=>$now_time,'page'=>$page,'page_size'=>USER_CENTER_PAGE_SIZE);
					if(count($list[$layer_id]) == 0)   $res['invite_user_account_list'] = array();
					else $res =  $this->borr->invite_user_account($where);
					$data['invi_user'] = $res['invite_user_account_list'];
					$data['user_details'] = '1';
					$this->load->library('pagination');
                    $config['base_url'] ='/user/invite_layer/'.$layer_id.'.html?';
                    $config['total_rows'] = $res['total_rows'];
                    $config['per_page'] = USER_CENTER_PAGE_SIZE;
                    $this->pagination->initialize($config);
					$data['dou'] = $invite_layer[$layer_id];
					$data['layer_id'] = $layer_id;
					if($typer == 'exel'){
						$this->load->library('PHPExcel');
						$this->load->library('PHPExcel/IOFactory'); 
						$objPHPExcel = new PHPExcel();
						$objPHPExcel->getProperties()->setTitle("用户资金统计")->setDescription(COMPANY_NAME); 
						$objPHPExcel->setActiveSheetIndex(0);
						$titles = array("推荐人号", "用户", "注册时间", "历史投资额", "历史提成额", "上月投资额", "上月提成额", "本月投资额", "本月提成额");
						$rett = $res['invite_user_account_list'];
						for($j = 0; $j < count($titles); $j ++){
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j, 1, $titles[$j]);
						}
						for($j = 0; $j < count($rett); $j++){
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $j+2, $rett[$j]['inv_userid']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $j+2, strdisplay_start($rett[$j]['username'],2));
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $j+2, $rett[$j]['add_time']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $j+2, $rett[$j]['money']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $j+2, $rett[$j]['money']*$data['dou']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $j+2, $rett[$j]['last_money']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $j+2, $rett[$j]['last_money']*$data['dou']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $j+2, $rett[$j]['now_money']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $j+2, $rett[$j]['now_money']*$data['dou']);
						}
			
						$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
						//发送标题强制用户下载文件 
						header('Content-Type: application/vnd.ms-excel'); 
						header('Content-Disposition: attachment;filename="推广用户详情.xls"'); 
						header('Cache-Control: max-age=0'); 
						$objWriter->save('php://output');
						return;
					}
				}
			}
			$data['tab'] = "user";
			$data['sub'] = "cps";
			$data['user_sub'] = "invi_user";
			$this->load->view('user/zhs_invite_account',$data);
		}
	}
	
	// ******************************
	function zhs_vipRank(){
		$data = array();
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		$this->load->model('User_model');
		$where['user_id'] = $user_id;
		$res = $this->User_model->vipRank($where);
		if(isset($res['maxscore'])){
			$next = $res['maxscore']+1;
		}
		else $next = null;
		$data['next_rank'] = $this->User_model->nextVipRank($next);
		$res['mol'] = $data['next_rank']['minscore'] - $res['invest_score'];
		$data['rank'] = $res;
		
		$data['title'] = "积分等级".' - '.CON_KEYWORDS;
		$data['sub'] = "user";
		$data['user_sub'] = "user_score";
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "积分等级";
		$data['sub_2_href'] = "/user/zhs_vipRank.html";
		
		$this->load->view('user/zhs_user_viprank',$data);
	}
	
	
	//****** 20141201 by liub 
	public function zhs_user_safe(){
		$this->load->model('User_model','user');
		$user = $this->session->userdata('rsh_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){	
			$user = $this->user->get_by_uid($user['user_id']);
			$data['user'] = $user;
			$data['title'] = "安全认证".' - '.CON_KEYWORDS;
			$data['tab'] = $this->tab;
			$data['sub'] = 'user';
			$data['user_sub'] = 'user_safe';
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "安全认证";
			$data['sub_2_href'] = "/user/zhs_user_safe.html";
			$this->load->view('user/zhs_user_safe',$data);
			
		}else{
			header('Location: /login.html');
		}	
	}
	//宝付授权操作
	function page_Accredit(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model', 'user');
		$user = $this->user->get_username_by_id($user['user_id']);
		if($user['real_status'] == '2'){
			$this->load->library('baofooSdk/baofooRequestService',null,'br');
			$user_id = $user['user_id'];
			$service_url = 'http://www.runshi.com/callback/page_Accredit_service';
			$page_url = 'http://www.runshi.com/user/zhs_user_safe.html';
			$this->br->page_Accredit($user_id,$service_url,$page_url);
		}
		elseif($user['real_status'] == '0') {
			$this->js->alert('请先完成[实名认证-安全绑定]');
			$this->js->go("/user/zhs_realname.html");
		}
		else $this->js->go("/user/zhs_user_safe.html");
	}
	
	// 获取宝付的验证码
	function submit_bind_code(){
		//发送手机验证码
		$ret['info'] = '1';
		echo json_encode($ret);
		exit;
	}

	//设置专属客服
	function set_user_kefu(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('kf_model');
		$this->load->model('user_kf_model');
		//判断用户是否为客服
		/*
		$check_kefuid=$this->kf_model->get_one(array('userid'=>$user['user_id']));
		if($check_kefuid){
			echo '此号是客服号，不能设置专属客服';
			exit;
		}
		*/
		if($_POST['kefu']){
			//判断用户是否为已经设置了专属客服
			$check_userid=$this->user_kf_model->get_one(array('userid'=>$user['user_id']));
			if($check_userid){
				echo '你已经设置过专属客服';
				exit;
			}
			$data=array();
			$data['userid']=$user['user_id'];
			$data['kf_id']=$_POST['kefu'];
			$data['change_num']=1;
			$data['addtime']=time();
			$add_res=$this->user_kf_model->add($data);
			if($add_res>=1){
				echo "设置成功";		
			}else{
				echo '设置失败,请重新设置';
			}
		}else{
			echo '设置失败';
			exit;
		}		
	}

	//记录用户咨询专属客服点击数
	public function user_click_kefu(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('user_kf_model');
		$result=$this->user_kf_model->user_click($user['user_id']);
		echo $result;
		if($result>0){
			echo '点击成功！';
		}else{
			echo '获取点击失败';
		}
	}
}
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {
    
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$user = $this->session->userdata('rsh_user');
		if(!$user['user_id']){
			$this->js->go("/login/index.html");
			exit;
		}
		$this->tab = 'user';
		$this->sub = 'user';
	}
	
	public function index(){
		self::zhs_center();
	}
	
	
	public function logout(){
		setcookie("zhsjr_online",false,0,'/');
		//$this->session->unset_userdata('rsh_user');
		$this->session->sess_destroy();
		$this->js->go('/user.html');
	}
	

	
	/**
	 * 用户中心
	 */
	public function zhs_center(){

		// 标的安全等级
		$this->load->model('Conset_model');
		$this->load->model('Borrow_model');
		$data['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();	
		$data['borrow_style'] 	= $this->Borrow_model->arrays('borrow_style');//还款方式
		$data['borrow_bill_bank'] 				= $this->Borrow_model->arrays('borrow_bill_bank');//银行
		$data['borrow_status'] 				= $this->Borrow_model->arrays('borrow_status');//状态
		$data['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//标类型
		$user = $this->session->userdata('rsh_user');
		$adata['page_start'] = intval($this->input->get('per_page'));//intval($page);
		$adata['page_size'] = 8;
		$adata['where']['user_id'] 	= $user['user_id'];
		$adata['where']['fstatus'] 	= "0,1";
			
		// 成功投资的标
		// $index_borrow_list_key = 'rsh_center_tender_list';
		
		$tender_vdata= $this->Borrow_model->borrow_tender_user_list($adata);
		//投标实际总金额
		$sj_account = 0;
		if( !empty($tender_vdata['tender_list']) ){
			foreach( $tender_vdata['tender_list'] as $k => $v ){
				$sj_account += $v['account'];
			}
		}
		$data['tender_money_count'] = $sj_account;
		$data['tender_list']= $tender_vdata['tender_list'];//成功投资的列表
		
		//$this->mycache->set_array($index_borrow_list_key,$bdata3,60);
		
		$this->load->library('pagination');
		$config['base_url'] = '/user/zhs_center.html?order=';
		$config['total_rows'] = $tender_vdata['tender_count'];
		$config['per_page'] = 8;
		//$config['page_query_string'] = false;
		
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		
		$this->pagination->initialize($config);
		
		

		// 正在投资的借款标
		$index_borrow_list_key = 'rsh_center_borrow_list';
		$bdata2 = null;
		if (!$bdata2){
			$adata['page_size']  = 5;
			$adata['page_start'] = 0;
			$now_vdata = $this->Borrow_model->get_borrow_blist($adata);
			//投标实际总金额
			$sj_account = 0;
			if( !empty($now_vdata['borrow_list']) ){
				foreach( $now_vdata['borrow_list'] as $k => $v ){
					if($v['account'] <= $v['account_yes']) $v['fstatus'] = 3;
				}
			}
			$data['tender_money_count'] = $sj_account;
			$bdata2['borrow_list']= $now_vdata['borrow_list'];//成功投资的列表
		}
		$data['borrow_list'] = $bdata2['borrow_list'];
		
		$data['user_sub'] = 'tender_myinv';
		$data['sub'] = 'tender';
		//$data['addup']= $safeinfo['addup'];
		$data['title'] 						= "用户中心".' - '.CON_KEYWORDS; 
		$data['tab'] 						= "user"; 
		$data['sub_1'] = "会员中心";
		$data['sub_1_href'] = "/user/zhs_center.html";
		$data['sub_2'] = "我的投资";
		$data['sub_2_href'] = "/user/zhs_center.html";
		$this->load->view('user/zhs_user_center',$data);
	}
	
	/**
	 * 实名认证
	 */
	public function zhs_realname(){

		$data = array();
		$act = $this->input->post('act');
		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model', 'user');
		$this->load->model('Area_model', 'area');
		$this->load->model('Linkage_model', 'linkage');
		$this->load->model('User_info_model', 'user_info');
		$data = $this->get_user_info('user_realname','safe');
		if(isSet($data['user']['real_status']) && $data['user']['real_status'] == '2'){
			$this->js->go('/user/zhs_user_safe.html');exit;
		}
		if($act == 'submit'){
			$ret = array(
				'msg' => '',
				'url' => '',
			);
			
			
			$setArr = array(
				'realname' => $this->input->post('realname'),
				'sex'  => $this->input->post('sex'),
				//'nation' => $this->input->post('nation'),
				'birthday' => "",
				'card_type' => 1,//默认使用身份证
				'card_id' => $this->input->post('card_id'),
				//'province' => $this->input->post('province'),
				//'city' => $this->input->post('city'),
				//'area' => $this->input->post('area'),
				//'area' => $this->input->post('area'),
				//'qq' => $this->input->post('qq'),
			);
			
			/* $thumbnail = Array();
			$file_upload  =  "f_file";		
			$thumbnail = $this->upload_borrow_thumbnail(UPFILES_BORROW_DEAL,$file_upload); */
			
		  //echo "34131";exit;	
			if(!$setArr['realname']){
				$ret['msg'] = '请填写真实姓名';
			}elseif(!$setArr['sex']){
				$ret['msg'] = '请选择性别';
			}elseif(!$setArr['card_id']){
				$ret['msg'] = '请填写证件号码';
			}elseif(!$this->myformcheck->isChinese($setArr['realname'])){
				$ret['msg'] = '请填写有效的真实姓名';
			}else{
				$isE = $this->user->get_one(array('card_id'=>$setArr['card_id'],'user_id !='=>$user['user_id']),'user_id');
				if($isE){
					$ret['msg'] = '证件号码已存在';
				}
			}
			if($ret['msg']){
				$this->js->alert($ret['msg']);
				exit;
			}
			// 20150430 身份证正反面上传验证
			$user = $this->user->get_username_by_id($user['user_id']);
			if(!isset($user['realname']) &&  !isset($user['card_id'])) {
				$this->user->update($setArr, array('user_id' => $user['user_id']));
			}
			
			$rest = $this->idcardcheck($setArr['card_id'],$setArr['realname']);
			
			if($rest['ResponseCode'] == '100'){
				$wert = $rest['Identifier'];
				if($wert['Result'] =='一致'){
					$setArr['birthday'] = $wert['Birthday'];
					$setArr['real_status'] = 1;//实名成功
					$this->user->update($setArr, array('user_id' => $user['user_id']));
					$ret['msg'] = '操作成功';
					$this->js->alert($ret['msg']);
					$this->js->go("/user/zhs_center.html");
					exit;
				}
				else $this->js->alert('返回错误结果：'.$wert['Result']);
			}
			else $this->js->alert('返回错误码：'.$rest['ResponseCode']);
			
			//end tangcai
		}else{
			
			//$data['user'] = $this->user->get_by_uid($user['user_id']);
			$data['user_info'] = $this->user_info->get_by_uid($user['user_id']);
			$data['area'] = $this->area->get_list();
			$data['minzu'] = $this->linkage->get_linkage_list(31);
			$data['title'] = "实名认证-安全绑定".' - '.CON_KEYWORDS;
			$data['v'] = $this->user->arrays();
			$data['sub'] = 'user';
			$data['user_sub'] = 'user_safe';
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "安全认证";
			$data['sub_2_href'] = "/user/zhs_user_safe.html";
			$data['sub_3'] = "实名认证-安全绑定";
			$data['sub_3_href'] = "/user/zhs_realname.html";
			$this->load->view('/user/zhs_user_realname',$data);
		}
	}
	
	private function idcardcheck($idcardNum,$realname){
		header("content-type:text/html; charset=utf8");
		$client = new SoapClient ("http://service.sfxxrz.com/IdentifierService.svc?wsdl");
		$r = new stdClass();
		$r->IDNumber = $idcardNum;
		$r->Name = $realname;
		$c = new stdClass();
		$c->UserName = IDCARDUSERNAME;
		$c->Password = IDCARDUSERPWD;
		$result_json  =  $client->SimpleCheckByJson(array("request"=>json_encode($r),"cred"=>json_encode($c)))->SimpleCheckByJsonResult;
		$result = json_decode($result_json, true);
		return $result;
	}
	
	// 上传文件并打水印
	private function upload_borrow_thumbnail($path,$file_upload){
		if(!file_exists($path)){
			mkdir($path,0777);
		}
			
		$config['upload_path'] = $path;
		$config['allowed_types'] = 'jpg|png';
		$config['file_name'] = 'cardpic_'.uniqid();
		$config['overwrite'] = true;
		$config['max_size'] = '512';
		$config['max_width']  = '2048';
		$config['max_height']  = '3000';
		$this->load->library('upload', $config);
		
		$data = $this->upload->multiple($file_upload);
		//echo $file_upload;exit;
		$name = Array();
		$i = 0;
		
		foreach ($data['files'] as $value) {
			$thumbnail = $path.$value['file_name'];
			// 添加水印				
			$this->load->library('image_lib');		
            $warter['source_image'] = $thumbnail;
			$warter['wm_type'] = 'overlay';
			//******  by liub  20141211 修改水印图片，以及水印位置为 底部居右
			$warter['wm_overlay_path'] = WM_OVERLAY_FILE_PNG;
			$warter['wm_opacity'] = '60';
			$warter['wm_vrt_alignment'] = 'B';// 底部
			$warter['wm_hor_alignment'] = 'R';// 右边
			// ****************** end 
			//$this->image_lib->initialize($warter);
			//溥$this->image_lib->watermark();
			$name[$i] = $thumbnail;
			$i++;
		}
		
		return $name;
	}
	
	/**
	 * 邮箱认证
	 */
	public function zhs_email(){
		$data = array();
		$act = $this->input->post('act');
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		$this->load->model('User_model', 'user');
		$data = $this->get_user_info('user_email','safe');
		//$data['user'] = $this->user->get_by_uid($user_id);
		if($act == 'submit'){
			$typer = $this->input->post('typer');
			$ret = array(
				'msg' => '',
				'url' => '',
			);
			$email = $this->input->post('emai');
			if($data['user']['email_status']==1  && !isset($typer)){
				$ret['msg'] = '邮箱验证已通过';
				echo json_encode($ret);
				exit;
			}
			if(isset($typer)){
				if($typer == 'oldemail'){
					$active_id = $this->session->userdata('zhsjr_active_id__email');
					$old_verify_code = $this->input->post('old_verify_code');
					if($active_id != $old_verify_code){
						$ret['msg'] = '旧邮箱验证码不正确';
						echo json_encode($ret);
						exit;
					}
				}
				else  if($typer == 'phone'){
					$active_id = $this->session->userdata($user ['phone']);
					$verify_code = $this->input->post('verify_code');
					if($active_id != $verify_code){
						$ret['msg'] = '手机验证码不正确';
						echo json_encode($ret);
						exit;
					}
				}
			}
			
			if(!$this->myformcheck->isEmail($email)){
				$ret['msg'] = '请输入有效邮箱';
			}else{
				$where = array(
					'user_id !=' => $user_id,
					'email' => $email
				);
				if($this->user->get_one($where, 'user_id')){
					$ret['msg'] = '邮箱已存在';
				}
			}
			
			if($ret['msg']){
				echo json_encode($ret);
				exit;
			}
			
			$setArr = array(
				'email' => $email,
				'email_status' => 2,
			);
			$this->user->update($setArr,array('user_id'=>$user_id));	
			$res = $this->notice_email(10,$user_id);
			
			if($res){
				$ret['cont'] = "邮箱发送成功，请尽快登录邮箱激活";
				echo json_encode($ret);
				exit;
			}else{
				$ret['msg'] = '邮件发送失败，请联系客服';
				echo json_encode($ret);
				exit;
			}
				
			
		}elseif($act == 'apply'){
			$res = $this->notice_email(11,$user_id);
			if($res){
				$ret['info'] =1;
				echo json_encode($ret);
				exit;
			}else{
				$ret['msg'] = '邮件发送失败，请联系客服';
				echo json_encode($ret);
				exit;
			}
		}else{
			$data['title'] = "邮箱认证".' - '.CON_KEYWORDS;
			$data['sub'] = 'user';
			$data['user_sub'] = 'user_safe';
			
			
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "安全认证";
			$data['sub_2_href'] = "/user/zhs_user_safe.html";
			$data['sub_3'] = "邮箱认证";
			$data['sub_3_href'] = "/user/zhs_email.html";
			$this->load->view('user/zhs_user_email',$data);
		}
	}
	
	/**
	 * 手机认证
	 */
	public function zhs_phone(){

		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model', 'user');
		$user_id = $user['user_id'];
		$this->mycache->delete('rsh_user_'.$user_id);
		$data = $this->get_user_info('user_phone','safe');
		if($data['user']['real_status']  != '0'){
			$this->js->go('/user/zhs_user_safe.html');
			exit;
		}
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		
		if($this->input->post('act') == 'apply'){//申請
			$k = md5('zhsjr');
			if(isset($_COOKIE[$k]) && $_COOKIE[$k] == 2){
				$ret['msg'] = '3分钟内只能获取一次短信验证码，请稍后再试';
				echo json_encode($ret); 
				exit;				
			}else{
				$phone = $this->input->post('phone');
				if(!$this->myformcheck->isPhone($phone)){
					$ret['msg'] = '请填写有效的手机号码';
				}else{
					// 验证手机号码是否存在
					$tmpwhere = array(
						'phone'=>$phone
					);
					$row = $this->user->get_one($tmpwhere,'user_id');
					if(isset($row['user_id'])){
						$ret['msg'] = '手机号码已存在';
					}
				}
				if($ret['msg']){
					echo json_encode($ret); 
					exit;				
				}
				$ctime = time()+3*60;//300秒内只能发送一次短信息
				setcookie($k,1,$ctime);
				
				$md5 = md5($user['user_id'].$phone);
				$md5 = substr($md5,3,6);
				$msg = "您的手机验证码为：".$md5;
				$this->load->library('phone');
				$res = $this->phone->send($phone,$msg);
				if($res == '0'){
					
					$this->user->update ( array ('smscode' => $md5, 'smstime' => time () ), array ('user_id' => $user['user_id'] ) );
					
					$ret['msg'] = '手机验证码已发送，请注意查收';
					echo json_encode($ret); 
					exit;
				}else{
					$ret['msg'] = '短信发送失败，请稍后再试或联系客服';
					echo json_encode($ret); 
					exit;
				}
			}
		}elseif($this->input->post('act') == 'verify'){//更换验证
			$verify_code = $this->input->post('verify_code');
			$phone = $this->input->post('phone');
			$md5 = substr(md5($user_id.$phone),3,6);
			if($verify_code == $md5){
				$typer = $this->input->post('typer');
				if($typer == 'oldphone'){//根据旧手机验证码更换
					$md5 = $this->session->userdata ($user ['phone']);
					$old_verify_code = $this->input->post('old_verify_code');
					if($old_verify_code != $md5){
						$ret['msg'] = '旧手机验证码错误';
						echo json_encode($ret);
						exit; 
					}
				}
				elseif($typer == 'paypw'){//根据交易手机验证码更换
					$cardid = $this->input->post('cardid');
					$paypassword = $this->input->post('paypassword');
					// 验证手机号码是否存在
					$tmpwhere = array(
						'card_id'=>$cardid,
						'paypassword'=>md5(md5($paypassword))
					);
					$row = $this->user->get_one($tmpwhere,'user_id');
					if(!isset($row['user_id'])){
						$ret['msg'] = '身份证或者交易密码错误';
						echo json_encode($ret);
						exit; 
					}
				}
				$setArr = array(
					'phone_status' => 1,
					'phone'=>$phone
				);
				$res = $this->user->update($setArr,array('user_id'=>$user_id));
				if($res){
					
					$user['phone'] = $phone;
					$this->send_msg(array('user_id'=>$user_id,sys_id=>18));//发送代金卷站内信
					//$this->notice_credit($user_id, 'phone');
					$ret['msg'] = '验证通过';
					$ret['url'] = '/user/zhs_phone';
					echo json_encode($ret);
					exit; 
				}else{
					$ret['msg'] = '网络忙，请稍后再试！';
					echo json_encode($ret);
					exit;
				}
			}else{
				$ret['msg'] = '新手机验证码错误';
				echo json_encode($ret);
				exit;
			}
			
		}else{//展示页面
			$data['sub'] = 'user';
			$data['user_sub'] = 'user_safe';
			$data['title'] = "手机认证".' - '.CON_KEYWORDS;
			
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "安全认证";
			$data['sub_2_href'] = "/user/zhs_user_safe.html";
			$data['sub_3'] = "手机认证";
			$data['sub_3_href'] = "/user/zhs_phone.html";
			
			$this->load->view('user/zhs_user_phone',$data);
		}
	}


	/**
	 * 上传头像
	 */
	public function zhs_photo($act = ''){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model', 'user');
		$user_id = $user['user_id'];
		$data['user'] = $this->user->get_by_uid($user_id);
		
		if($act == 'upfile'){
			$file_src = "src.png"; 
			$filename162 = "./static/data/avatar/".$user_id."_avatar_big.jpg"; 
			$filename48 = "./static/data/avatar/".$user_id."_avatar_middle.jpg";
			$filename20 = "./static/data/avatar/".$user_id."_avatar_small.jpg"; 
			
			$src=base64_decode($_POST['pic']);
			$pic1=base64_decode($_POST['pic1']);   
			$pic2=base64_decode($_POST['pic2']);  
			$pic3=base64_decode($_POST['pic3']);  
			
			if($src) {
				file_put_contents($file_src,$src);
			}
			
			file_put_contents($filename162,$pic1);
			file_put_contents($filename48,$pic2);
			file_put_contents($filename20,$pic3);
			
			$rs['status'] = 1;
			
			print json_encode($rs);
		}else{
			$data = $this->get_user_info('user_photo','safe');
			$data['title'] = "上传头像".' - '.CON_KEYWORDS;
			$data['sub_1'] = "上传头像";
			$data['sub_1_href'] = "/user/zhs_photo.html";
			$this->load->view('user/zhs_user_photo',$data);
		}
	}

	/**
	 * 用户详细信息
	 * @param string $act 
	 */
	public function zhs_info($act=''){
		$data = array();
		
		$submit = $this->input->post('submit');
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		
		$this->load->model('User_model', 'user');
		$this->load->model('User_info_model', 'userinfo');
		$data = $this->get_user_info('','user_base');
		$data['act']=$act;
		//$data['user'] = $this->user->get_by_uid($user_id);
		$data['userinfo'] = $this->userinfo->get_by_uid($user_id);
		if($act == 'contact'){
			//education_record
			if($submit == 1){
				$username0 = trim($this->input->post('username0'));
				if($user['username'] != $username0){
					if($username0=='' || $this->myformcheck->isNumber($username0)){
						$ret['msg'] = '会员昵称不允许纯数字或者为空';
						$ret['url'] = '';
						echo json_encode($ret);
						exit;
					}
					$is_username= $this->user->check_username($username0);
					if($is_username){
						$ret['msg'] = '此会员昵称已经存在';
						$ret['url'] = '';
						echo json_encode($ret);
						exit;
					}
					else {
						$user['username']= $username0;
						$this->session->set_userdata('rsh_user',$user);
					}
				}
				
				$setArr = array(
					'education_record' => intval($this->input->post('education_record')),
					'education_school' => $this->input->post('education_school'),
					'education_time1' => strtotime($this->input->post('education_time1')),
					'education_time2' => strtotime($this->input->post('education_time2')),
					'marry' => intval($this->input->post('marry')),
					'child' => intval($this->input->post('child')),
					'safe' => intval($this->input->post('safe')),
					'house' => intval($this->input->post('house')),
					'car' => intval($this->input->post('car')),
					'province' => intval($this->input->post('province')),
					'city' => intval($this->input->post('city')),
					'area' => intval($this->input->post('area')),
					'address' => $this->input->post('address'),
					'tel' => $this->input->post('tel'),
					'qq' => $this->input->post('qq'),
					'wangwang' => $this->input->post('wangwang'),
					'linkman1' => $this->input->post('linkman1'),
					'relation1' => intval($this->input->post('relation1')),
					'phone1' => $this->input->post('phone1'),
					'linkman2' => $this->input->post('linkman2'),
					'relation2' => intval($this->input->post('relation2')),
					'phone2' => $this->input->post('phone2'),
				);
				if($data['userinfo']){
					$res = $this->userinfo->update($setArr,array('user_id'=>$user_id));
				}else{
					$setArr['user_id'] = $user_id;
					$res = $this->userinfo->add($setArr);
				}
				if($setArr['address'] !="" && $setArr['qq'] !=""  && $setArr['linkman1'] !=""  && $setArr['linkman2'] !=""  && $setArr['education_record'] !=""  && $setArr['education_school']!="" ){
					//发放代金券
					$this->load->model('Redmanagement_model', 'redmanagement');
					$this->redmanagement->send_red($user_id,7,"用户信息完整");
				}
				$this->user->update(array('qq'=>$setArr['qq'],'username'=>$username0),array('user_id'=>$user_id));
				$ret['msg'] = '个人详细信息提交成功';
				$ret['url'] = '/user/zhs_info/contact.html';
				echo json_encode($ret);
				exit;
			}
			$data['user_tab'] = 'userinfo_contact';
			$data['title'] = "个人详细信息".' - '.CON_KEYWORDS;
			
		}else{
			$this->js->alert('页面不存在');
			$this->js->go('/user/zhs_info/contact.html');
		}
		$this->load->model('Area_model', 'area');
		$data['area'] = $this->area->get_list();
		$data['sub'] = 'user';
		$data['user_sub'] = "user_msg";
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "个人信息";
		$data['sub_2_href'] = "/user/zhs_info/contact.html";
		$this->load->view('user/zhs_user_info',$data);
	}

	
	
	public function zhs_pwd(){
		$act = $this->input->post('act');
		if($act == 'submit'){
			$ret = array('msg'=>'','url'=>'');
			$oldpwd = $this->input->post('oldpwd');
			$newpwd = $this->input->post('newpwd');
			$newpwd2 = $this->input->post('newpwd2');
			
			if(!isset($newpwd[5])){
				$ret['msg'] = '新密码太短';
				echo json_encode($ret);
				exit;
			}elseif (!$this->myformcheck->isName ($newpwd)) {
				$ret ['msg'] = '无效密码，必须[数字+字母]，其中只允许[_~!#$^&?]字符';
				echo json_encode ($ret);
				exit ;
			}
			if($newpwd == $newpwd2){
				$user = $this->session->userdata('rsh_user');
				$user_id = $user['user_id'];
				$this->load->model('User_model', 'user');
				$user = $this->user->get_by_uid($user_id);
				if(md5(md5($oldpwd)) == $user['password']){
					$md5 = md5(md5($newpwd));
					if($newpwd != $oldpwd && $md5!=$user['paypassword']){
						$res = $this->user->update(array('password'=>$md5),array('user_id'=>$user_id));
						if($res){
							//$this->send_msg(array('user_id'=>$user_id,sys_id=>19));//发送代金卷站内信
							$ret['url'] = '/user/logout/index.html';
							echo json_encode($ret);
							exit;
						}
					}else{
						$ret['msg'] = '新密码不允许同旧密码、登录密码相同';
						echo json_encode($ret);
						exit;
					}
					
				}else{
					$ret['msg'] = '旧密码验证失败,请确定再试';
					echo json_encode($ret);
					exit;
				}
			}else{
				$ret['msg'] = '两次更新密码输入不一致';
				echo json_encode($ret);
				exit;
			}
		}
	
		$data = $this->get_user_info('user_pwd','pwd');
		$data['sub'] = 'user';
		$data['user_sub'] = 'user_safe';
		$data['title'] = "密码设置".' - '.CON_KEYWORDS;
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "安全认证";
		$data['sub_2_href'] = "/user/zhs_user_safe.html";
		$data['sub_3'] = "登录密码";
		$data['sub_3_href'] = "/user/zhs_pwd.html";
		$this->load->view('user/zhs_user_pwd', $data);
	}
	
	public function zhs_paypwd(){
		if(!isset($_SESSION)){
		    session_start();
		}
		$act = $this->input->post('act');
		
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		$this->load->model('User_model', 'user');
		$user = $this->user->get_by_uid($user_id);
		
		$data['user'] = $user;
		if($act == 'submit'){
			$ret = array('msg'=>'','url'=>'');
			$verify_code = $this->input->post('ver_code');
			$md5 = $this->session->userdata ( $user ['phone'] );
			if ($verify_code =="" ){
				$ret['msg'] = '验证码不能为空';
				echo json_encode($ret);
				exit;
			}elseif ($verify_code!=$md5){
				$ret['msg'] = '验证码不正确';
				echo json_encode($ret);
				exit;
			}else{
				$oldpwd = $this->input->post('oldpwd');
				$newpwd = $this->input->post('newpwd');
				$newpwd2 = $this->input->post('newpwd2');
				
				if(!isset($newpwd[5])){
					$ret['msg'] = '新密码太短';
					echo json_encode($ret);
					exit;
				}elseif (!$this->myformcheck->isName ($newpwd)) {
					$ret ['msg'] = '无效密码，必须[数字+字母]，其中只允许[_~!#$^&?]字符';
					echo json_encode ($ret);
					exit ;
				}
				if($newpwd == $newpwd2){
					$tmpPwd = $user['paypassword'] ? $user['paypassword'] : $user['password'];
					if(md5(md5($oldpwd)) == $tmpPwd){
						$md5 = md5(md5($newpwd));
						if($md5 != $tmpPwd && $md5!=$user['password']){
							$res = $this->user->update(array('paypassword'=>$md5),array('user_id'=>$user_id));
							if($res){
								$this->send_msg(array('user_id'=>$user_id,sys_id=>20));//发送代金卷站内信
								$ret['url']= "/user/zhs_user_safe.html";
								echo json_encode($ret);
								exit;
							}
						}else {
							$ret['msg'] = '新密码不允许同旧密码、登录密码相同';
							echo json_encode($ret);
							exit;
						}
						
					}else{
						$ret['msg'] = '原始密码错误';
						echo json_encode($ret);
						exit;
					}
				}else{
					$ret['msg'] = '两次密码输入不一致';
					echo json_encode($ret);
					exit;
				}
			}
		}
		$data['sub'] = 'user';
		$data['user_sub'] = 'user_safe';
		$data['title'] = "交易密码".' - '.CON_KEYWORDS;
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "安全认证";
		$data['sub_2_href'] = "/user/zhs_user_safe.html";
		$data['sub_3'] = "交易密码";
		$data['sub_3_href'] = "/user/zhs_paypwd.html";
		$this->load->view('user/zhs_user_paypwd',$data);
	}
	
	public function zhs_find(){
		if(!isset($_SESSION)){
		    session_start();
		}
		$this->load->helper('captcha');
		$act = $this->input->post('act');
		
		$user = $this->session->userdata('rsh_user');
		$data['user'] = $user;
		
		if($act == 'mail'){
			$ret = array('msg'=>'','url'=>'');
			$captcha = $this->input->post('captcha');
			
			if ($captcha =="" ){
				$ret['msg'] = '验证码不能为空';
				echo json_encode($ret);
				exit;
			}elseif ($captcha!=$_SESSION['captcha']){
				$ret['msg'] = '验证码不正确';
				echo json_encode($ret);
				exit;
			}
			
			$res = $this->notice_email(3,$user['user_id']);
			
			if($res){
				$ret['msg'] = '邮件发送成功，请从邮件内容里的链接地址重新设置交易密码，该链接有效时间1小时，请及时操作！';
			}else{
				$ret['msg'] = '邮箱发送失败，请稍后再试或者联系客服！';
			}
			echo json_encode($ret); 
			exit;
		}
		$data = $this->get_user_info('user_paypwd','pwd');
		
		$data['title'] = "密码找回".' - '.CON_KEYWORDS;
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "安全认证";
		$data['sub_2_href'] = "/user/zhs_user_safe.html";
		$data['sub_3'] = "支付密码找回";
		$data['sub_3_href'] = "/user/zhs_find/index.html";
		$this->load->view('user/zhs_user_find',$data);
	}
	
	//*******************************
	function zhs_invite(){
		$data = array();
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		if($user['user_id']){
                        $page = intval($this->input->get('per_page'));
			$this->load->model('User_model');
			$data = $this->get_user_info('','invite','invite');
			$data['invite_user_list'] = $this->User_model->get_list(array('invite_userid'=>$user_id),'user_id,username,phone,invite_money,add_time');
			$data['url'] = 'http://www.okydt.com/?invite='.$user_id;
                        $invite=$this->User_model->get_invist_num($user_id,$page);
                        //$data['invite']=$invite['count']
                        $this->load->library('pagination');
                        $config['base_url'] ='/user/zhs_invite.html?';
                        $config['total_rows'] = intval($invite['count']);
                        $config['per_page'] = USER_CENTER_PAGE_SIZE;
                        $this->pagination->initialize($config);

			
			$data['title'] = "推广码".' - '.CON_KEYWORDS;
			$data['sub'] = "cps";
			$data['user_sub'] = "cps_user";
			$data['sub_1'] = "理财推广";
			$data['sub_1_href'] = "/user/zhs_invite.html";
			$data['sub_2'] = "推广账号";
			$data['sub_2_href'] = "/user/zhs_invite.html";
            $data['invite_user_list']=$invite['invite_user_list'];
			$this->load->view('user/zhs_user_invite',$data);
		}
	}
	
	/* 推荐用户分层 计算 */
	function invite_layer($layer_id = 0){
		
		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model','user');
		$user =  $this->user->get_by_uid($user['user_id']);
		if($user['is_self'] == '1'){
			$invite_layer = $this->user->arrays('invite_layer');//拿到分层信息
			$this->load->model('borrow_model','borr');
			$keys = array_keys($invite_layer);
			$last_time = date('Ym',strtotime("-1 month"));
			$now_time = date('Ym',time());
			$typer = $this->input->get('typer');
			if(!in_array($layer_id,$keys)){
				$sum = count($invite_layer);
				$users = array($user['user_id']);
				$daa = array();
				$list = array();
				$r = intval(date('m',time()));
				for($i=0; $i<$sum; $i++){
					$list[$keys[$i]] = $users;
					$val = $invite_layer[$keys[$i]];
					$where = array('invite_user'=>$users,'user_invite'=>'1','last_time'=>$last_time,'now_time'=>$now_time );
					$res =  $this->borr->invite_user_account($where);
					$res['invite_user_account_list']['money_ti']=  number_format($res['invite_user_account_list']['money']*$val,2);
					$res['invite_user_account_list']['last_money_ti']=  number_format($res['invite_user_account_list']['last_money']*$val,2);
					$res['invite_user_account_list']['now_money_ti']=  number_format($res['invite_user_account_list']['now_money']*$val,2);
					for($j=1;$j<=$r;$j++){
						$res['invite_user_account_list']['month'.$j.'_money'] = number_format($res['invite_user_account_list']['month'.$j.'_money']/1000,2);
					}
					$res['invite_user_account_list']['layer_id'] = $keys[$i];
					$daa[$keys[$i]] = $res['invite_user_account_list'];
					$users = $res['users_id'];
					if(count($users) == 0) break;
				}
				$this->mycache->set_array('invite_layer_userid'.$user['user_id'],$list);
				if($typer == 'exel'){
					
					$this->load->library('PHPExcel');
					$this->load->library('PHPExcel/IOFactory'); 
					$objPHPExcel = new PHPExcel();
					
					$objPHPExcel->getProperties()->setTitle("推广用户汇总")->setDescription(COMPANY_NAME); 
					$objPHPExcel->setActiveSheetIndex(0);
					$titles = array("推荐等级","推荐用户数","历史投资额","历史提成额","上月投资额","上月提成额","本月投资额","本月提成额");
					for($j = 0; $j < count($titles); $j ++){
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j, 1, $titles[$j]);
					}
					for($j = 0; $j < count($daa); $j++){
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $j+2, "第".($j+1)."级");
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $j+2, $daa[$j+1]['count_userid']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $j+2, $daa[$j+1]['money']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $j+2, $daa[$j+1]['money_ti']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $j+2, $daa[$j+1]['last_money']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $j+2, $daa[$j+1]['last_money_ti']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $j+2, $daa[$j+1]['now_money']);
						$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $j+2, $daa[$j+1]['now_money_ti']);
					}
					
					$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
					
					//发送标题强制用户下载文件 
					header('Content-Type: application/vnd.ms-excel'); 
					header('Content-Disposition: attachment;filename="推广用户汇总.xls"'); 
					header('Cache-Control: max-age=0'); 
					$objWriter->save('php://output'); 
					return;
				}
				$data['invi_user'] = $daa;
			}
			else {
				$page = intval($this->input->get('per_page'));
				if(!$page) $page = 0;
				$list = $this->mycache->get_array('invite_layer_userid'.$user['user_id']);
				if(isset($list[$layer_id])){
					if($typer == 'exel'){
						$page = null;
					}
					$where = array('invite_user'=>$list[$layer_id],'user_details'=>'1','last_time'=>$last_time,'now_time'=>$now_time,'page'=>$page,'page_size'=>USER_CENTER_PAGE_SIZE);
					if(count($list[$layer_id]) == 0)   $res['invite_user_account_list'] = array();
					else $res =  $this->borr->invite_user_account($where);
					$data['invi_user'] = $res['invite_user_account_list'];
					$data['user_details'] = '1';
					$this->load->library('pagination');
                    $config['base_url'] ='/user/invite_layer/'.$layer_id.'.html?';
                    $config['total_rows'] = $res['total_rows'];
                    $config['per_page'] = USER_CENTER_PAGE_SIZE;
                    $this->pagination->initialize($config);
					$data['dou'] = $invite_layer[$layer_id];
					$data['layer_id'] = $layer_id;
					if($typer == 'exel'){
						$this->load->library('PHPExcel');
						$this->load->library('PHPExcel/IOFactory'); 
						$objPHPExcel = new PHPExcel();
						$objPHPExcel->getProperties()->setTitle("用户资金统计")->setDescription(COMPANY_NAME); 
						$objPHPExcel->setActiveSheetIndex(0);
						$titles = array("推荐人号", "用户", "注册时间", "历史投资额", "历史提成额", "上月投资额", "上月提成额", "本月投资额", "本月提成额");
						$rett = $res['invite_user_account_list'];
						for($j = 0; $j < count($titles); $j ++){
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j, 1, $titles[$j]);
						}
						for($j = 0; $j < count($rett); $j++){
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $j+2, $rett[$j]['inv_userid']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $j+2, strdisplay_start($rett[$j]['username'],2));
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $j+2, $rett[$j]['add_time']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $j+2, $rett[$j]['money']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $j+2, $rett[$j]['money']*$data['dou']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $j+2, $rett[$j]['last_money']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $j+2, $rett[$j]['last_money']*$data['dou']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $j+2, $rett[$j]['now_money']);
							$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $j+2, $rett[$j]['now_money']*$data['dou']);
						}
			
						$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
						//发送标题强制用户下载文件 
						header('Content-Type: application/vnd.ms-excel'); 
						header('Content-Disposition: attachment;filename="推广用户详情.xls"'); 
						header('Cache-Control: max-age=0'); 
						$objWriter->save('php://output');
						return;
					}
				}
			}
			$data['tab'] = "user";
			$data['sub'] = "cps";
			$data['user_sub'] = "invi_user";
			$this->load->view('user/zhs_invite_account',$data);
		}
	}
	
	// ******************************
	function zhs_vipRank(){
		$data = array();
		$user = $this->session->userdata('rsh_user');
		$user_id = $user['user_id'];
		$this->load->model('User_model');
		$where['user_id'] = $user_id;
		$res = $this->User_model->vipRank($where);
		if(isset($res['maxscore'])){
			$next = $res['maxscore']+1;
		}
		else $next = null;
		$data['next_rank'] = $this->User_model->nextVipRank($next);
		$res['mol'] = $data['next_rank']['minscore'] - $res['invest_score'];
		$data['rank'] = $res;
		
		$data['title'] = "积分等级".' - '.CON_KEYWORDS;
		$data['sub'] = "user";
		$data['user_sub'] = "user_score";
		
		$data['sub_1'] = "我的账户";
		$data['sub_1_href'] = "/user/zhs_info/contact.html";
		$data['sub_2'] = "积分等级";
		$data['sub_2_href'] = "/user/zhs_vipRank.html";
		
		$this->load->view('user/zhs_user_viprank',$data);
	}
	
	
	//****** 20141201 by liub 
	public function zhs_user_safe(){
		$this->load->model('User_model','user');
		$user = $this->session->userdata('rsh_user');
		if(isset($user['user_id']) && intval($user['user_id']) > 0){	
			$user = $this->user->get_by_uid($user['user_id']);
			$data['user'] = $user;
			$data['title'] = "安全认证".' - '.CON_KEYWORDS;
			$data['tab'] = $this->tab;
			$data['sub'] = 'user';
			$data['user_sub'] = 'user_safe';
			$data['sub_1'] = "我的账户";
			$data['sub_1_href'] = "/user/zhs_info/contact.html";
			$data['sub_2'] = "安全认证";
			$data['sub_2_href'] = "/user/zhs_user_safe.html";
			$this->load->view('user/zhs_user_safe',$data);
			
		}else{
			header('Location: /login.html');
		}	
	}
	//宝付授权操作
	function page_Accredit(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('User_model', 'user');
		$user = $this->user->get_username_by_id($user['user_id']);
		if($user['real_status'] == '2'){
			$this->load->library('baofooSdk/baofooRequestService',null,'br');
			$user_id = $user['user_id'];
			$service_url = 'http://www.runshi.com/callback/page_Accredit_service';
			$page_url = 'http://www.runshi.com/user/zhs_user_safe.html';
			$this->br->page_Accredit($user_id,$service_url,$page_url);
		}
		elseif($user['real_status'] == '0') {
			$this->js->alert('请先完成[实名认证-安全绑定]');
			$this->js->go("/user/zhs_realname.html");
		}
		else $this->js->go("/user/zhs_user_safe.html");
	}
	
	// 获取宝付的验证码
	function submit_bind_code(){
		//发送手机验证码
		$ret['info'] = '1';
		echo json_encode($ret);
		exit;
	}

	
	//设置专属客服
	function set_user_kefu(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('kf_model');
		$this->load->model('user_kf_model');
		//判断用户是否为客服
		/*
		$check_kefuid=$this->kf_model->get_one(array('userid'=>$user['user_id']));
		if($check_kefuid){
			echo '此号是客服号，不能设置专属客服';
			exit;
		}
		*/
		if($_POST['kefu']){
			//判断用户是否为已经设置了专属客服
			$check_userid=$this->user_kf_model->get_one(array('userid'=>$user['user_id']));
			if($check_userid){
				echo '你已经设置过专属客服';
				exit;
			}
			$data=array();
			$data['userid']=$user['user_id'];
			$data['kf_id']=$_POST['kefu'];
			$data['change_num']=1;
			$data['addtime']=time();
			$add_res=$this->user_kf_model->add($data);
			if($add_res>=1){
				echo "设置成功";		
			}else{
				echo '设置失败,请重新设置';
			}
		}else{
			echo '设置失败';
			exit;
		}		
	}	

	//记录用户咨询专属客服点击数
	public function user_click_kefu(){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('user_kf_model');
		$result=$this->user_kf_model->user_click($user['user_id']);
		echo $result;
		if($result>0){
			echo '点击成功！';
		}else{
			echo '获取点击失败';
		}
	}
}
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
